import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('@/components/Login')
  },
  {
    path: '/home',
    component: () => import('@/components/Home'),
    redirect: '/home/welcome',
    children: [
      {
        path: 'welcome',
        component: () => import('@/components/Welcome')
      },
      {
        path: 'users',
        component: () => import('@/views/users/users')
      },
      {
        path: 'rights',
        component: () => import('@/views/rights/rights')
      },
      {
        path: 'roles',
        component: () => import('@/views/rights/roles')
      },
      {
        path: 'goods',
        component: () => import('@/views/goods/goods')
      },
      {
        path: 'goods/addgoods',
        component: () => import('@/views/goods/addgoods')
      },
      {
        path: 'params',
        component: () => import('@/views/goods/params')
      },
      {
        path: 'categories',
        component: () => import('@/views/goods/categories')
      },
      {
        path: 'orders',
        component: () => import('@/views/orders/orders')
      },
      {
        path: 'reports',
        component: () => import('@/views/reports/reports')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 拿到token判断是否具有token
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router
